package com.hospital.service.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.service.project.entity.help.ProGroupInfoHelp;
import com.hospital.service.project.model.param.*;
import com.hospital.service.project.model.vo.ProGroupInfoVo;
import com.hospital.service.project.model.vo.ProGroupPageInfoVo;
import com.hospital.service.project.service.ProInfoBaseService;
import com.hospital.service.publics.entity.PubFilePath;
import com.hospital.service.publics.service.PubFilePathService;
import common.core.constant.Constant;
import common.core.entity.GroupEntity;
import common.core.entity.ProjectEntity;
import common.core.entity.ProjectImportSuperEntity;
import common.core.util.CommonUtil;
import common.core.util.CustomizeRuntimeException;
import common.core.util.ExcelUtil;
import enhance.plus.logic.LogicBaseServiceImpl;
import com.hospital.service.project.entity.ProGroupInfo;
import com.hospital.service.project.mapper.ProGroupInfoMapper;
import com.hospital.service.project.service.ProGroupInfoService;
import enhance.plus.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 实验分组表 接口
*
* @author lfb
* @created 2020-10-19 09:02:05
*/
@SuppressWarnings("all")
@Service
public class ProGroupInfoServiceImpl extends LogicBaseServiceImpl<ProGroupInfoMapper, ProGroupInfo> implements ProGroupInfoService{

    @Autowired
    private ProGroupInfoMapper proGroupInfoMapper;

    @Autowired
    private PubFilePathService pubFilePathService;

    @Autowired
    private ProInfoBaseService proInfoBaseService;

    @Override
    public String getGroupNumber() {
        return proInfoBaseService.getCode("pro_group_info",Constant.PROJECT_GROUP_PREFIX,"group_number");
    }

    /**
     * 分页查找
     * @param param
     * @return
     */
    @Override
    public ProGroupPageInfoVo queryInfo(ProGroupInfoQueryPageParam param) {
        if (CommonUtil.isEmpty(param)) {
            throw new CustomizeRuntimeException("传入的参数位空");
        }
        return this.proGroupInfoMapper.queryInfo(param);
    }

    /**
     * 实验分组导入
     * @param file
     */
    @Transactional
    @Override
    public void importGroup(MultipartFile file) {
        log.debug("判断文件的格式是否是excel文件");
        if (!ExcelUtil.isExcel(file)) {
            throw CustomizeRuntimeException.exception("传入的文件格式不是excel");
        }

        log.debug("读取文件中的内容，并存入实体类中");
        List<GroupEntity> groupEntities = null;
        try {
            groupEntities = ExcelUtil.importExcel(file, 0, 1, GroupEntity.class);
        } catch (Exception e) {
            throw CustomizeRuntimeException.exception("文件上传失败");
        }
        log.debug("将数据存入数据库");
        for (GroupEntity groupEntitie : groupEntities) {
            if (CommonUtil.isEmpty(groupEntitie)) {
                continue;
            }
            // 设置项目编号
            ProGroupInfo proGroupInfo = CommonUtil.copyBean(groupEntitie, ProGroupInfo.class);
            this.save(proGroupInfo);
            pubFilePathService.fileCreate(proGroupInfo.getGroupNumber(),proGroupInfo.getProjectNumber());
        }

    }

    @Override
    public void groupExport(ProGroupInfoQueryListParam param, HttpServletResponse response) {
        log.debug("找到此条数据");
        if (CommonUtil.isEmpty(param)) {
            throw CustomizeRuntimeException.exception("传入的参数为空");
        }
        List<GroupEntity> list = this.proGroupInfoMapper.getData(param.getProjectNumber());
        String title = param.getProjectNumber() + "-GroupInfo";
        try {
            ExcelUtil.exportExcel(list, title, title, GroupEntity.class, title + ".xls", response);
        } catch (Exception e) {
            throw CustomizeRuntimeException.exception("信息导出失败");
        }

    }

    /**
     * 增加实验分组
     * @param param
     */
    @Transactional
    @Override
    public void add(ProGroupInfoParam param) {
        //保存
        this.save(CommonUtil.copyBean(param,ProGroupInfo.class));
        pubFilePathService.fileCreate(param.getGroupNumber(),param.getProjectNumber());
    }

}
